<template>
  <!-- sync 需要去除，因为dialog不在父组件里面，无法共用数据，避免直接更改属性，因为每当父组件重新渲染时，该值将被覆盖。-->
  <!-- :before-close和@close都能用 -->
  <el-dialog
    title="新增员工"
    :visible="visible"
    width="50%"
    :before-close="closeFn"
  >
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" /> -->
        <el-select
          ref="deptSelect"
          v-model="formData.departmentName"
          placeholder="请选择部门"
          @focus="getDepts"
        >
          <!-- 饿了么官方要求有属性，所以给一个空值占位 -->
          <!-- v-loading饿了么官方提供的全局组件，在哪里都能用 -->
          <el-option v-loading="treeLoading" class="treeOption" value="">
            <el-tree
              :data="depts"
              :props="defaultProps"
              default-expand-all
              @node-click="nodeClick"
            />
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Employee from '@/contant/employee'
import { getDepartments } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import { addEmployee } from '@/api/employees'

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeLoading: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''

      },
      rules: {
        username: [{ required: true, message: '不能为空！' }, { min: 1, max: 4, message: '请输入1-4位' }],
        mobile: [{ required: true, message: '不能为空！' }, { pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/, message: '格式不正确！' }],
        formOfEmployment: [{ required: true, message: '不能为空！' }],
        workNumber: [{ required: true, message: '不能为空！' }],
        departmentName: [{ required: true, message: '不能为空！' }],
        timeOfEntry: [{ required: true, message: '不能为空！' }]

      },
      // template里用的数据都是需要挂载到vue实例身上的，所以外引入的数据用不了，在data里重新声明一个变量接
      hireType: Employee.hireType,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }

    }
  },

  created() { },

  methods: {
    closeFn() {
      // 关闭 dialog时，情况表单数据，清除校验，关闭弹窗
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''

      }
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    async getDepts() {
      try {
        this.treeLoading = true
        const { depts } = await getDepartments()
        this.depts = tranListToTreeData(depts, '')
        this.treeLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    nodeClick(data) {
      this.formData.departmentName = data.name
      // 手动触发失焦，选择框隐藏
      this.$refs.deptSelect.blur()
    },
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        await addEmployee(this.formData)
        this.$message.success('新增员工成功')
        this.closeFn()
        // this.$emit('updateEmployeeList')
        // 子向父传值this.$parent直接调用父亲身上的方法，常用的还是$emit
        this.$parent.getEmployeeList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.el-select-dropdown__item.hover,
.el-select-dropdown__item:hover .el-select-dropdown__item {
  background-color: #fff;
  overflow: unset;
}
.treeOption {
  height: 100px;
}
</style>
